<template>
  <div>
    <div id="myChart" style="width: 100%; height: 300px" />
  </div>
</template>

<script>
export default {
  name: "chart1",
  data () {
    return {
      chart1: null
    }
  },
  mounted() {

    let _this = this
    window.addEventListener("resize", function() {
      if (_this.chart1 == null) {
        _this.chart1 = _this.$echarts.init(document.getElementById('myChart'))
      }
      _this.chart1.getDom().style.height = ( (document.body.clientHeight - 187) / 2) + "px"
      _this.chart1.resize()
    })

    this.$nextTick(() => {
      this.initChart()
    })
  },

  beforeDestroy() {
    if (!this.chart1) {
      return
    }
    this.chart1.dispose()
    this.chart1 = null
  },

  methods: {


    initChart() {

      let chart = this.$echarts.init(document.getElementById('myChart'))

      this.chart1 = chart

      // 动态设置高度
      chart.getDom().style.height = ( (document.body.clientHeight - 187) / 2) + "px"

      let option = {
        title: {
          top: '5%',
          text: '设备分析',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '15%',
          left: 'center'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '60%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 100, name: '在线' },
              { value: 2, name: '离线' },
            ]
          }
        ]
      }

      chart.setOption(option)
      chart.resize()
    }
  }
}
</script>

<style scoped>
.title {
  font-size: 20px;
  margin-left: 2%;
  margin-top: 1%;
  z-index: 999;
  color: black;
}
</style>
